<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="90px"
    >
      <el-form-item :label="$t('staff.search')" prop="postName">
        <el-input
          v-model="queryParams.postName"
          :placeholder="$t('operating.pleaseWrite')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('history.deptname')" prop="postCode">
        <treeselect
          v-model="queryParams.deptValue"
          :placeholder="$t('operating.pleaseChosen')"
          :show-count="true"
          :max-height="600"
          :options="depData"
          :default-expand-level="1"
          style="width: 150px; margin-right: 10px"
          @input="nodeClick"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >{{ $t("operating.search") }}</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("operating.reset")
        }}</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:car:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >{{ $t('common.caddkey') }}</el-button>
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

          >{{ $t('common.cdelete') }}</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:car:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >{{ $t("operating.export") }}</el-button>
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >{{$t('car.seachCarType')}}</el-button>
      </el-col> -->
    <!-- <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList"
      /> -->
    </el-row>

    <el-table
      ref="multipleTable"
      class="datalist"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <!--多选框-->
      <el-table-column
        type="selection"
        min-width="50"
        align="center"
      />
      <!--车辆名称-->
      <!-- <el-table-column
        prop="name"
        :label="$t('car.carName')"
        min-width="150"
      ></el-table-column> -->
      <el-table-column
        min-width="150"
        :label="$t('car.carName')"
      >
        <template slot-scope="scope">
          <span :style="scope.row.onlineState==0?'color:#E64373':'color:#43DAE6' ">（{{ scope.row.onlineState==0?$t('common.coffline'):$t('common.conline') }}）</span>{{ scope.row.name }}
        </template>
      </el-table-column>
      <!--车辆编号-->
      <el-table-column
        prop="resourceId"
        :label="$t('car.carId')"
        min-width="150"
      />
      <!--车辆类型-->
      <el-table-column
        prop="workTypeName"
        :label="$t('car.carType')"
        min-width="200"
      />
      <!--绑定标签-->
      <el-table-column
        prop="tagId"
        :label="$t('car.tagId')"
        min-width="150"
      />
      <!--所在部门-->
      <el-table-column
        prop="departmentName"
        :label="$t('staff.departmentName')"
        min-width="150"
      />
      <!-- 在线状态 -->
      <!-- <el-table-column
        min-width="100"
        :label="$t('staff.onlineState')"
        prop="onlineState"
      >
        <template slot-scope="scope">
          <el-button
            icon="el-icon-check"
            :type="scope.row.onlineState === 1 ? 'success' : 'info'"
            circle
          ></el-button>
        </template>
      </el-table-column> -->
      <!--操作-->
      <el-table-column
        prop="batteryTime"
        :label="$t('staff.batteryTime')"
        min-width="150"
      />
      <el-table-column
        prop="batteryValue"
        :label="$t('staff.batteryValue')"
        min-width="100"
      />

      <el-table-column
        :label="$t('operating.operating')"
        min-width="235px"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="primary"
            @click="seedetails(scope.row)"
            >{{ $t("operating.seeDetails") }}</el-button
          > -->
          <el-button
            v-hasPermi="['system:car:detail']"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="seedetails(scope.row)"
          >{{
            $t("operating.seeDetails")
          }}</el-button>
          <el-button
            v-hasPermi="['system:car:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate( scope.row)"
          >{{ $t("operating.handle") }}</el-button>
          <el-button
            v-hasPermi="['system:car:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >{{ $t("operating.delete") }}</el-button>
          <!-- <el-button
            size="mini"
            type="primary"
            @click="handleUpdate(scope.row)"
            >{{ $t("operating.handle") }}</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            >{{ $t("operating.delete") }}</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item
          :label="$t('staff.departmentName')"
          prop="depIdx"
          :rules="filter_rules({ required: '请选择部门' })"
        >
          <treeselect
            v-model="form.depIdx"
            :options="deptSelect"
            :show-count="true"
            :placeholder="$t('department.inputmentName')"
          />
          <!-- <el-select
                  v-model="form.departmentId"
                  filterable
                  :placeholder="$t('operating.pleaseChosen')"
                >
                  <el-option
                    v-for="item in deptSelect"
                    :key="item.idx"
                    :label="item.name"
                    :value="item.idx"
                  />
                </el-select> -->
        </el-form-item>

        <!-- <el-form-item
          :label="$t('staff.departmentName')"
          prop="depIdx"
          :rules="filter_rules({ required: '请选择部门' })"
        >
          <el-select
            v-model="form.depIdx"
            filterable
            :placeholder="$t('operating.pleaseChosen')"
          >
            <el-option
              v-for="item in deptSelect"
              :key="item.idx"
              :label="item.name"
              :value="item.idx"
            />
          </el-select>
        </el-form-item> -->
        <!--&lt;!&ndash;车辆名称&ndash;&gt;-->
        <!--<el-form-item label="车辆名称" prop="name" :rules="filter_rules({required:'请输入车辆名称'})">-->
        <!--<el-input v-model="form.name" style="width: 215px;" placeholder="请输入车辆名称"></el-input>-->
        <!--</el-form-item>-->
        <!--&lt;!&ndash;车辆类型&ndash;&gt;-->
        <!--<el-form-item label="车辆类型" :rules="filter_rules({required:'请输入车辆类型'})">-->
        <!--<el-input v-model="form.carType" style="width: 215px;" :disabled="true" placeholder="请输入车辆类型"></el-input>-->
        <!--</el-form-item>-->
        <!--车辆信息-->
        <el-form-item
          :label="$t('car.carName')"
          :rules="filter_rules({ required: '请输入车辆名称' })"
        >
          <el-input
            v-model="form.name"

            :placeholder="$t('operating.pleaseWrite')"
          />
        </el-form-item>
        <!--车辆类型-->
        <el-form-item :label="$t('car.carType')">
          <el-select
            v-model="form.carTypeId"
            filterable
            :placeholder="$t('operating.pleaseChosen')"
          >
            <el-option
              v-for="item in carTypeList"
              :key="item.idx"
              :label="item.name"
              :value="item.idx"
            />
          </el-select>
        </el-form-item>
        <!--绑定标签-->
        <el-form-item
          :label="$t('car.tagId')"
          :rules="filter_rules({ required: '请输入绑定标签' })"
        >
          <el-input
            v-model="form.tagId"

            :placeholder="$t('operating.pleaseWrite')"
          />
        </el-form-item>
        <!--绑定标签-->
        <el-form-item
          :label="$t('car.carId')"
          :rules="filter_rules({ required: '请输入车辆编号' })"
        >
          <el-input
            v-model="form.resourceId"

            :placeholder="$t('operating.pleaseWrite')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('operating.determine'), }}</el-button>
        <el-button @click="cancel">{{ $t('operating.cancel') }}</el-button>
      </div>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog

      :visible.sync="details"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="部门名称" prop="postName">
          <span>{{ form.depIdx }}</span>
        </el-form-item>
        <el-form-item label="车辆名称" prop="postCode">
          <span>{{ form.name }}</span>
        </el-form-item>
        <el-form-item label="车辆类型" prop="postCode">
          <span>{{ form.carTypeId }}</span>
        </el-form-item>

        <el-form-item label="绑定标签" prop="postSort">
          <span>{{ form.tagId }}</span>
        </el-form-item>
        <el-form-item label="车辆RFID编号" prop="postCode">
          <span>{{ form.resourceId }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="details = false">{{ $t('operating.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import i18n from '@/utils/lang/index'
import Treeselect from '@riophae/vue-treeselect'
import { treeselect } from '@/api/system/dept'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {
  carfindPage,
  department,
  findListCarType,
  departmentlist,
  carsave,
  cardelete
} from '@/api/system/staff'

export default {
  name: 'Post',
  components: { Treeselect },
  data() {
    return {
      depData: [],
      carTypeList: [],
      deptSelect: [],
      carIdx: null,
      // 部门下拉
      details: false,
      tableHeight: window.innerHeight - 300,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: '',
        deptValue: undefined,
        departmentId: undefined

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: '岗位名称不能为空', trigger: 'blur' }
        ],
        postCode: [
          { required: true, message: '岗位编码不能为空', trigger: 'blur' }
        ],
        postSort: [
          { required: true, message: '岗位顺序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.deparlist()
  },
  methods: {
    //  获取部门列表
    getDept() {
      treeselect().then((response) => {
        this.deptSelect = response.data
      })
    },

    //  获取车辆类型列表
    getCarTypeList() {
      findListCarType().then(res => {
        this.carTypeList = res.data
      })
    },
    deparlist() {
      treeselect().then((response) => {
        this.depData = response.data
      })
    },

    nodeClick(val) {
      const self = this
      // 可将地图的ID设置为树结构的ID，通过点击节点，来切换地图
      this.queryParams.departmentId = val
      // this.getStaffList();
    },
    // 查看详情
    seedetails(row) {
      this.details = true
      console.log(row)
      this.form = {
        depIdx: row.departmentName,
        name: row.name,
        carTypeId: row.workTypeName,
        resourceId: row.resourceId,
        tagId: row.tagId
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      carfindPage(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: '',
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      const arr = selection.map((item) => item.idx)

      this.ids = []

      arr.forEach((element) => {
        this.ids.push({ idx: element })
      })
      console.log(this.ids)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加车辆'
      this.getDept()
      this.getCarTypeList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true
      this.getDept()
      this.getCarTypeList()
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.carIdx = row.idx
      this.form = {
        depIdx: row.departmentId,
        name: row.name,
        carTypeId: row.workTypeId,
        resourceId: row.resourceId,
        tagId: row.tagId
      }
      this.title = '修改车辆'
      // 设置编辑框为可见状态
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title != '修改车辆') {
            const param = {
              // "carType" :this.form.carType,
              name: this.form.name,
              tagId: this.form.tagId,
              type: 'car',
              resourceId: this.form.resourceId,
              workTypeId: this.form.carTypeId,
              departmentId: this.form.depIdx
            }
            carsave(param).then((response) => {
              if (response.code == '200') {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            const param = {
              name: this.form.name,
              tagId: this.form.tagId,
              resourceId: this.form.resourceId,
              type: 'car',
              idx: this.carIdx,
              workTypeId: this.form.carTypeId,
              departmentId: this.form.depIdx
            }
            carsave(param).then((response) => {
              if (response.code == '200') {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const that = this
      let roleIds = []
      if (row.idx) {
        roleIds = [{ idx: row.idx }]
      } else {
        roleIds = that.ids
      }

      this.$confirm(i18n.t('sxm.delTipItem'), i18n.t('menuManage.title'), {
        confirmButtonText: i18n.t('operating.determine'),

        cancelButtonText: i18n.t('operating.cancel'),
        type: 'warning'
      })
        .then(function() {
          cardelete(roleIds).then((response) => {
            if (response.code == 200) {
              that.getList()
              that.msgSuccess(response.msg)
            } else {
              that.msgError(response.msg)
            }
          })
        })
        .then(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/car/export',
        {

        },
        `post_${new Date().getTime()}.xls`
      )
    }
  }
}
</script>
